package com.lc1993929.tank.util;

import com.lc1993929.tank.astar.AStar;
import com.lc1993929.tank.constants.*;
import com.lc1993929.tank.entity.tank.Tank;
import com.lc1993929.tank.entity.vo.DirectionAndDistance;
import com.lc1993929.tank.entity.vo.TankAndCoord;
import com.lc1993929.tank.map.Coord;
import com.lc1993929.tank.map.Node;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.lc1993929.tank.constants.MapElement.*;

/**
 * @author LiuChang
 * @date 2018/11/25 10:30
 */
@Slf4j
public class MapUtil {
    /**
     * 判断坐标是否在目标坐标的九宫格内
     */
    public static boolean nearTarget(Coord from, Coord target) {
        return getSqrt(from, target) <= 3;
    }

    /**
     * 获取两个坐标之间的直线距离
     */
    public static double getSqrt(Coord from, Coord target) {
        return Math.sqrt(Math.pow(target.x - from.x, 2) + Math.pow(target.y - from.y, 2));
    }

    /**
     * 获取己方受到敌方威胁值的总和
     */
    public static Integer getEnemyValueSum() {
        Integer myAllValue = 0;
        // 判断敌方能否打到我方坦克
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0 && !Condition.gameMap.canAttackMyList(Condition.enemyArmata1).isEmpty()) {
            myAllValue = myAllValue + 1;
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0 && !Condition.gameMap.canAttackMyList(Condition.enemyArmata2).isEmpty()) {
            myAllValue = myAllValue + 1;
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0 && !Condition.gameMap.canAttackMyList(Condition.enemyT90).isEmpty()) {
            myAllValue = myAllValue + 1;
        }
        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0 && !Condition.gameMap.canAttackMyList(Condition.enemyK2Panther).isEmpty()) {
            myAllValue = myAllValue + 1;
        }
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0 && !Condition.gameMap.canAttackMyList(Condition.enemyWZ123).isEmpty()) {
            myAllValue = myAllValue + 1;
        }
        return myAllValue;
    }

    /**
     * 获取敌方受到我方威胁值的总和
     */
    public static Integer getMyValueSum() {
        Integer enemyAllValue = 0;
        if (Condition.myArmata1.shengyushengming > 0 && !MapUtil.canAttackEnemyList(Condition.myArmata1).isEmpty()) {
            enemyAllValue = enemyAllValue + 1;
        }
        if (Condition.myArmata2.shengyushengming > 0 && !MapUtil.canAttackEnemyList(Condition.myArmata2).isEmpty()) {
            enemyAllValue = enemyAllValue + 1;
        }
        if (Condition.myK2Panther.shengyushengming > 0 && !MapUtil.canAttackEnemyList(Condition.myK2Panther).isEmpty()) {
            enemyAllValue = enemyAllValue + 1;
        }
        if (Condition.myT90.shengyushengming > 0 && !MapUtil.canAttackEnemyList(Condition.myT90).isEmpty()) {
            enemyAllValue = enemyAllValue + 1;
        }
        if (Condition.myWZ123.shengyushengming > 0 && !MapUtil.canAttackEnemyList(Condition.myWZ123).isEmpty()) {
            enemyAllValue = enemyAllValue + 1;
        }
        return enemyAllValue;
    }

    /**
     * 判断我方是否是受到威胁值最大的坦克
     */
    public static boolean isMyMaxValue(Tank tank) {
        Integer myArmata1Value = Condition.gameMap.getEnemyValue(Condition.myArmata1.coord);
        Integer myArmata2Value = Condition.gameMap.getEnemyValue(Condition.myArmata2.coord);
        Integer myK2PantherValue = Condition.gameMap.getEnemyValue(Condition.myK2Panther.coord);
        Integer myT90Value = Condition.gameMap.getEnemyValue(Condition.myT90.coord);
        Integer myWZ123Value = Condition.gameMap.getEnemyValue(Condition.myWZ123.coord);
        Integer myValue = Condition.gameMap.getEnemyValue(tank.coord);
        return myValue >= myArmata1Value
                && myValue >= myArmata2Value
                && myValue >= myK2PantherValue
                && myValue >= myT90Value
                && myValue >= myWZ123Value;
    }

    /**
     * 根据坦克状态和目标确定移动或攻击和方向
     */
    public static Action getAction(Tank tank) {
        if (tank.tankStatus == TankStatus.SUERGENCE || tank.tankStatus == TankStatus.SEARCH || tank.tankStatus == TankStatus.TARGET
                || tank.tankStatus == TankStatus.FORMATION || tank.tankStatus == TankStatus.WEI_SHA) {
            if (tank.coord.equals(tank.targetCoord)) {
                //    坦克已经到达目标点，随便来一炮
                log.error(tank.name + "坦克已到达目标点");
                Direction direction = getRandomDirection();
                Action action = Action.builder().direction(direction).type(Action.Type.ATTACK).distance(tank.shecheng).useGlod(false).build();
                refreshAttack(tank, action.getDirection(), action.getDistance());
                tank.action = action;
                return action;
            }
            Condition.gameMap.start = new Node(tank.coord);
            Condition.gameMap.end = new Node(tank.targetCoord);
            if (tank.yidong > 1) {
                DirectionAndDistance directionAndDistance = AStar.ArmataFindNextStep(Condition.gameMap);
                if (directionAndDistance == null || directionAndDistance.getDirection() == Direction.WAIT) {
                    //    无法到达目标点，不知道怎么办，随便来一炮
                    log.error(tank.name + "坦克没有任何目标");
                    Direction direction = getRandomDirection();
                    Action action = Action.builder().direction(direction).type(Action.Type.ATTACK).distance(tank.shecheng).useGlod(false).build();
                    refreshAttack(tank, action.getDirection(), action.getDistance());
                    tank.action = action;
                    return action;
                } else {
                    Action action = Action.builder().direction(directionAndDistance.getDirection()).type(Action.Type.MOVE).distance(directionAndDistance.getDistance()).useGlod(false).build();
                    refreshMap(tank, action.getDirection(), action.getDistance());
                    tank.action = action;
                    return action;
                }
            } else {
                Direction direction = AStar.findNextStep(Condition.gameMap);
                if (direction == null) {
                    //    无法到达目标点，不知道怎么办，随便来一炮
                    log.error(tank.name + "坦克没有任何目标");
                    direction = getRandomDirection();
                    Action action = Action.builder().direction(direction).type(Action.Type.ATTACK).distance(tank.shecheng).useGlod(false).build();
                    refreshAttack(tank, action.getDirection(), action.getDistance());
                    tank.action = action;
                    return action;
                }
                Action action = Action.builder().direction(direction).type(Action.Type.MOVE).distance(tank.yidong).useGlod(false).build();
                refreshMap(tank, action.getDirection(), action.getDistance());
                tank.action = action;
                return action;
            }
        } else if (tank.tankStatus == TankStatus.ATTACK) {
            Action action = Action.builder().direction(tank.targetDirection).type(Action.Type.ATTACK).distance(tank.shecheng).useGlod(false).build();
            refreshAttack(tank, action.getDirection(), action.getDistance());
            tank.action = action;
            return action;
        } else if (tank.tankStatus == TankStatus.DIRECTION) {
            if (tank.targetDirection == Direction.WAIT) {
                Condition.gameMap.start = new Node(tank.coord);
                Condition.gameMap.end = new Node(MapUtil.getNearestEnemyCoord(tank));
                Direction direction = AStar.findNextStep(Condition.gameMap);
                Action action = Action.builder().direction(direction).type(Action.Type.ATTACK).distance(tank.shecheng).useGlod(false).build();
                refreshAttack(tank, action.getDirection(), action.getDistance());
                tank.action = action;
                return action;
            } else {
                Action action = Action.builder().direction(tank.targetDirection).type(Action.Type.MOVE).distance(tank.targetDistance).useGlod(false).build();
                refreshMap(tank, action.getDirection(), action.getDistance());
                tank.action = action;
                return action;
            }
        } else {
            //    不知道怎么办，随便来一炮
            log.error(tank.name + "坦克没有任何目标");
            Direction direction = getRandomDirection();
            Action action = Action.builder().direction(direction).type(Action.Type.ATTACK).distance(tank.shecheng).useGlod(false).build();
            refreshAttack(tank, action.getDirection(), action.getDistance());
            tank.action = action;
            return action;
        }
    }

    public static void refreshAttack(Tank tank, Direction direction, Integer distance) {
        if (direction == Direction.LEFT) {
            for (int i = 1; i <= tank.shecheng; i++) {
                Coord coord = new Coord(tank.coord.x - i, tank.coord.y);
                MapElement mapElement = Condition.gameMap.maps.get(coord);
                if (isEnemyTank(mapElement)) {
                    Tank enemyTank = getTankByMapElement(mapElement);
                    enemyTank.shengyushengming = enemyTank.shengyushengming - tank.gongji;
                    break;
                } else if (mapElement == BAR) {
                    break;
                }
            }
        } else if (direction == Direction.RIGHT) {
            for (int i = 1; i <= tank.shecheng; i++) {
                Coord coord = new Coord(tank.coord.x + i, tank.coord.y);
                MapElement mapElement = Condition.gameMap.maps.get(coord);
                if (isEnemyTank(mapElement)) {
                    Tank enemyTank = getTankByMapElement(mapElement);
                    enemyTank.shengyushengming = enemyTank.shengyushengming - tank.gongji;
                    break;
                } else if (mapElement == BAR) {
                    break;
                }
            }
        } else if (direction == Direction.UP) {
            for (int i = 1; i <= tank.shecheng; i++) {
                Coord coord = new Coord(tank.coord.x, tank.coord.y - i);
                MapElement mapElement = Condition.gameMap.maps.get(coord);
                if (isEnemyTank(mapElement)) {
                    Tank enemyTank = getTankByMapElement(mapElement);
                    enemyTank.shengyushengming = enemyTank.shengyushengming - tank.gongji;
                    break;
                } else if (mapElement == BAR) {
                    break;
                }
            }
        } else if (direction == Direction.DOWN) {
            for (int i = 1; i <= tank.shecheng; i++) {
                Coord coord = new Coord(tank.coord.x, tank.coord.y + i);
                MapElement mapElement = Condition.gameMap.maps.get(coord);
                if (isEnemyTank(mapElement)) {
                    Tank enemyTank = getTankByMapElement(mapElement);
                    enemyTank.shengyushengming = enemyTank.shengyushengming - tank.gongji;
                    break;
                } else if (mapElement == BAR) {
                    break;
                }
            }
        }
    }

    public static void refreshMap(Tank tank, Direction direction, Integer distance) {
        Condition.gameMap.maps.put(tank.oldCoord, WAY);
        refreshCoordByDirectionAndDistance(tank.coord, direction, distance);
        Condition.gameMap.updateByMyTankMove(tank);
        Condition.gameMap.maps.put(tank.coord, getMapElementByTank(tank));
        Condition.gameMap.updateByMyTank(tank);
    }

    public static void refreshCoordByDirectionAndDistance(Coord coord, Direction direction, Integer distance) {
        if (direction == Direction.LEFT) {
            coord.x = coord.x - distance;
        } else if (direction == Direction.RIGHT) {
            coord.x = coord.x + distance;
        } else if (direction == Direction.UP) {
            coord.y = coord.y - distance;
        } else if (direction == Direction.DOWN) {
            coord.y = coord.y + distance;
        }
    }

    public static MapElement getMapElementByTank(Tank tank) {
        if (tank.tId.equals(Condition.myArmata1.tId)) {
            return MY_ARMATA1;
        } else if (tank.tId.equals(Condition.myArmata2.tId)) {
            return MY_ARMATA2;
        } else if (tank.tId.equals(Condition.myT90.tId)) {
            return MY_T90;
        } else if (tank.tId.equals(Condition.myK2Panther.tId)) {
            return MY_K2PANTHER;
        } else if (tank.tId.equals(Condition.myWZ123.tId)) {
            return MY_WZ123;
        } else if (tank.tId.equals(Condition.enemyArmata1.tId)) {
            return ENEMY_ARMATA1;
        } else if (tank.tId.equals(Condition.enemyArmata2.tId)) {
            return ENEMY_ARMATA2;
        } else if (tank.tId.equals(Condition.enemyT90.tId)) {
            return ENEMY_T90;
        } else if (tank.tId.equals(Condition.enemyK2Panther.tId)) {
            return ENEMY_K2PANTHER;
        } else if (tank.tId.equals(Condition.enemyWZ123.tId)) {
            return ENEMY_WZ123;
        }

        return null;
    }

    public static Tank getTankByMapElement(MapElement mapElement) {
        if (mapElement == MY_ARMATA1) {
            return Condition.myArmata1;
        } else if (mapElement == MY_ARMATA2) {
            return Condition.myArmata2;
        } else if (mapElement == MY_T90) {
            return Condition.myT90;
        } else if (mapElement == MY_K2PANTHER) {
            return Condition.myK2Panther;
        } else if (mapElement == MY_WZ123) {
            return Condition.myWZ123;
        } else if (mapElement == ENEMY_ARMATA1) {
            return Condition.enemyArmata1;
        } else if (mapElement == ENEMY_ARMATA2) {
            return Condition.enemyArmata2;
        } else if (mapElement == ENEMY_K2PANTHER) {
            return Condition.enemyK2Panther;
        } else if (mapElement == ENEMY_T90) {
            return Condition.enemyT90;
        } else if (mapElement == ENEMY_WZ123) {
            return Condition.enemyWZ123;
        }
        return null;
    }

    /**
     * 找到目标坐标是起始坐标的方向
     */
    public static Direction getDirection(Coord fromCoord, Coord targetCoord) {
        if (targetCoord.x > fromCoord.x && targetCoord.y == fromCoord.y) {
            return Direction.RIGHT;
        } else if (targetCoord.x < fromCoord.x && targetCoord.y == fromCoord.y) {
            return Direction.LEFT;
        } else if (targetCoord.y > fromCoord.y && targetCoord.x == fromCoord.x) {
            return Direction.DOWN;
        } else if (targetCoord.y < fromCoord.y && targetCoord.x == fromCoord.x) {
            return Direction.UP;
        } else {
            return Direction.WAIT;
        }
    }

    /**
     * 判断能否攻击到NPC
     */
    public static Direction attackNPC(Tank tank) {
        if (Condition.npc.coord != null && myCanAttack(tank, Condition.npc.coord)) {
            return getDirection(tank.coord, Condition.npc.coord);
        } else {
            return Direction.WAIT;
        }
    }

    /**
     * 判断我方坦克能否攻击到指定位置
     */
    public static boolean myCanAttack(Tank tank, Coord coord) {
        if (tank.coord.x == coord.x) {
            if (tank.shecheng >= Math.abs(tank.coord.y - coord.y)) {
                //坦克的坐标与目标点的横坐标距离相差比坦克的射程小
                if (tank.coord.y > coord.y) {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.y == tank.coord.y - i) {
                            return true;
                        }
                        int integer = myCanHaveValue(tank.coord.x, tank.coord.y - i);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                } else {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.y == tank.coord.y + i) {
                            return true;
                        }
                        int integer = myCanHaveValue(tank.coord.x, tank.coord.y + i);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                }
            }
        } else if (tank.coord.y == coord.y) {
            if (tank.shecheng >= Math.abs(tank.coord.x - coord.x)) {
                //坦克的坐标与目标点的横坐标距离相差比坦克的射程小
                if (tank.coord.x > coord.x) {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.x == tank.coord.x - i) {
                            return true;
                        }
                        int integer = myCanHaveValue(tank.coord.x - i, tank.coord.y);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                } else {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.x == tank.coord.x + i) {
                            return true;
                        }
                        int integer = myCanHaveValue(tank.coord.x + i, tank.coord.y);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                }
            }
        } else {
            //横坐标、纵坐标都不相等，无法攻击到
            return false;
        }
        return false;
    }

    public static boolean myCanAttack(Coord myTankCoord, Integer shecheng, Coord coord) {
        if (myTankCoord.x == coord.x) {
            if (shecheng >= Math.abs(myTankCoord.y - coord.y)) {
                //坦克的坐标与目标点的横坐标距离相差比坦克的射程小
                if (myTankCoord.y > coord.y) {
                    for (int i = 1; i < shecheng + 1; i++) {
                        if (coord.y == myTankCoord.y - i) {
                            return true;
                        }
                        int integer = myCanHaveValue(myTankCoord.x, myTankCoord.y - i);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                } else {
                    for (int i = 1; i < shecheng + 1; i++) {
                        if (coord.y == myTankCoord.y + i) {
                            return true;
                        }
                        int integer = myCanHaveValue(myTankCoord.x, myTankCoord.y + i);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                }
            }
        } else if (myTankCoord.y == coord.y) {
            if (shecheng >= Math.abs(myTankCoord.x - coord.x)) {
                //坦克的坐标与目标点的横坐标距离相差比坦克的射程小
                if (myTankCoord.x > coord.x) {
                    for (int i = 1; i < shecheng + 1; i++) {
                        if (coord.x == myTankCoord.x - i) {
                            return true;
                        }
                        int integer = myCanHaveValue(myTankCoord.x - i, myTankCoord.y);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                } else {
                    for (int i = 1; i < shecheng + 1; i++) {
                        if (coord.x == myTankCoord.x + i) {
                            return true;
                        }
                        int integer = myCanHaveValue(myTankCoord.x + i, myTankCoord.y);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                }
            }
        } else {
            //横坐标、纵坐标都不相等，无法攻击到
            return false;
        }
        return false;
    }

    //返回0：不可穿过，1：可攻击不可穿过，2：可穿过
    private static Integer myCanHaveValue(Integer x, Integer y) {
        // 是否在地图中
        if (x < 0 || x >= Condition.gameMap.width || y < 0 || y >= Condition.gameMap.hight) {
            return 0;
        }
        MapElement mapElement = Condition.gameMap.maps.get(new Coord(x, y));
        if (mapElement == MapElement.BAR) {
            return 0;
        }
        if (mapElement == ENEMY_ARMATA1
                || mapElement == MapElement.ENEMY_ARMATA2
                || mapElement == MapElement.ENEMY_K2PANTHER
                || mapElement == MapElement.ENEMY_T90
                || mapElement == MapElement.NPC
                || mapElement == MapElement.ENEMY_WZ123) {
            return 1;
        }
        return 2;
    }

    /**
     * 获取一个随机的方向
     */
    public static Direction getRandomDirection() {
        Integer random = Util.getRandom(4);
        Direction direction = Direction.WAIT;
        if (random == 0) {
            direction = Direction.LEFT;
        } else if (random == 1) {
            direction = Direction.RIGHT;
        } else if (random == 2) {
            direction = Direction.UP;
        } else if (random == 3) {
            direction = Direction.DOWN;
        }
        return direction;

    }

    /**
     * 找出能攻击到的敌人的列表
     */
    public static List<Tank> canAttackEnemyList(Tank tank) {
        Coord coord = tank.coord;
        List<Tank> enemyTanks = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            //威胁有4个方向
            for (int j = 1; j < tank.shecheng + 1; j++) {
                //左
                if (i == 0) {
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x - j, coord.y);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                } else if (i == 1) {
                    //    右
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x + j, coord.y);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                } else if (i == 2) {
                    //    上
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x, coord.y - j);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                } else {
                    //    下
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x, coord.y + j);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                }
            }
        }
        return enemyTanks;
    }

    public static List<Tank> canAttackEnemyList(Tank tank, Coord coord) {
        List<Tank> enemyTanks = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            //威胁有4个方向
            for (int j = 1; j < tank.shecheng + 1; j++) {
                //左
                if (i == 0) {
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x - j, coord.y);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                } else if (i == 1) {
                    //    右
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x + j, coord.y);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                } else if (i == 2) {
                    //    上
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x, coord.y - j);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                } else {
                    //    下
                    MapElement mapElement = Condition.gameMap.canAttackEnemy(coord.x, coord.y + j);
                    if (mapElement == BAR) {
                        break;
                    } else if (mapElement == WAY) {
                        continue;
                    } else {
                        enemyTanks.add(getTankByMapElement(mapElement));
                    }
                }
            }
        }
        return enemyTanks;
    }

    /**
     * 找到最近距离敌方坦克的坐标
     */
    public static Coord getNearestEnemyCoord(Tank tank) {
        Coord coord = tank.coord;
        double distance = Double.MAX_VALUE;
        Coord targetCoord = null;
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            double aramta1Distance = getSqrt(coord, Condition.enemyArmata1.coord);
            if (aramta1Distance <= distance) {
                distance = aramta1Distance;
                targetCoord = Condition.enemyArmata1.coord;
            }
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            double aramta2Distance = getSqrt(coord, Condition.enemyArmata2.coord);
            if (aramta2Distance <= distance) {
                distance = aramta2Distance;
                targetCoord = Condition.enemyArmata2.coord;
            }
        }
        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) {
            double k2PantherDistance = getSqrt(coord, Condition.enemyK2Panther.coord);
            if (k2PantherDistance <= distance) {
                distance = k2PantherDistance;
                targetCoord = Condition.enemyK2Panther.coord;
            }
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            double enemyT90Distance = getSqrt(coord, Condition.enemyT90.coord);
            if (enemyT90Distance <= distance) {
                distance = enemyT90Distance;
                targetCoord = Condition.enemyT90.coord;
            }
        }
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            double enemyWZ123Distance = getSqrt(coord, Condition.enemyWZ123.coord);
            if (enemyWZ123Distance <= distance) {
                distance = enemyWZ123Distance;
                targetCoord = Condition.enemyWZ123.coord;
            }
        }
        if (targetCoord == null) {
            targetCoord = new Coord(Math.round(Condition.gameMap.width / 2), Math.round(Condition.gameMap.hight / 2));
        }
        return targetCoord;
    }

    /**
     * 团战中的包围逻辑
     */
    public static void groupAttackSurround(List<Tank> enemyTanks, Tank myTank) {
        Tank inFireEnemyTank = findMinDistanceAndInFireEnemy(myTank);
        if (!enemyTanks.isEmpty()) {
            //如果有能直接打的坦克，直接打
            Tank bestTargetTank = enemyTanks.stream().min(Comparator.comparingInt(value -> {
                MapElement mapElement = getMapElementByTank(value);
                if (mapElement == ENEMY_ARMATA1 || mapElement == ENEMY_ARMATA2 || mapElement == ENEMY_T90) {
                    return value.shengyushengming;
                } else if (mapElement == MapElement.ENEMY_K2PANTHER) {
                    //优先攻击敌方黑豹
                    return 0;
                } else if (mapElement == MapElement.ENEMY_WZ123) {
                    //次要攻击敌方99
                    return 1;
                } else {
                    return Integer.MAX_VALUE;
                }
            })).get();
            myTank.tankStatus = TankStatus.ATTACK;
            myTank.targetDirection = getDirection(myTank.coord, bestTargetTank.coord);
        } else if (Condition.npc.coord != null && Condition.npc.shengyushengming > 0) {
            //    判断能不能攻击NPC
            if (myCanAttack(myTank, Condition.npc.coord)) {
                Integer enemyNpcValue = Condition.gameMap.getEnemyValue(Condition.npc.coord);
                if (enemyNpcValue <= 0) {
                    //  判断敌方是否能攻击到npc
                    myTank.tankStatus = TankStatus.ATTACK;
                    myTank.targetDirection = getDirection(myTank.coord, Condition.npc.coord);
                } else {
                    //    聚集在NPC周边就好
                    myTank.tankStatus = TankStatus.TARGET;
                    myTank.targetCoord = Condition.npc.coord;
                }
            } else {
                myTank.tankStatus = TankStatus.TARGET;
                myTank.targetCoord = Condition.npc.coord;
            }
        } else if (inFireEnemyTank != null) {
            //    如果有正在和友军交战的坦克，前去支援
            int allMyCanAttackNum = getAllMyCanAttack(inFireEnemyTank.coord).size();
            if (allMyCanAttackNum > 1 || myTank.shengyushengming > inFireEnemyTank.gongji) {
                //  判断会不会被对方一炮带走和己方有多少坦克正在围杀
                myTank.tankStatus = TankStatus.TARGET;
                myTank.targetCoord = inFireEnemyTank.coord;
            }
        } else {
            Tank aloneEnemyTank = findMinDistanceAloneEnemyTank();
            //     尝试围杀敌人
            List<TankAndCoord> tankAndCoords = canWeiShaAloneEnemyTank(aloneEnemyTank);
            if (tankAndCoords.size() > 1 && tankAndCoords.stream().anyMatch(tankAndCoord -> tankAndCoord.getTank().tId.equals(myTank.tId))) {
                myTank.tankStatus = TankStatus.WEI_SHA;
                myTank.targetCoord = tankAndCoords.stream().filter(tankAndCoord -> tankAndCoord.getTank().tId.equals(myTank.tId)).findAny().get().getCoord();
            }
        }
    }

    /**
     * 判断所有我方坦克中是否有正可以攻击目标地址的坦克
     */
    public static List<Tank> getAllMyCanAttack(Coord coord) {
        List<Tank> myTanks = new ArrayList<>();
        if (Condition.myArmata1.shengyushengming > 0 && myCanAttack(Condition.myArmata1, coord)) {
            myTanks.add(Condition.myArmata1);
        }
        if (Condition.myArmata2.shengyushengming > 0 && myCanAttack(Condition.myArmata2, coord)) {
            myTanks.add(Condition.myArmata2);
        }
        if (Condition.myT90.shengyushengming > 0 && myCanAttack(Condition.myT90, coord)) {
            myTanks.add(Condition.myT90);
        }
        if (Condition.myK2Panther.shengyushengming > 0 && myCanAttack(Condition.myK2Panther, coord)) {
            myTanks.add(Condition.myK2Panther);
        }
        if (Condition.myWZ123.shengyushengming > 0 && myCanAttack(Condition.myWZ123, coord)) {
            myTanks.add(Condition.myWZ123);
        }
        return myTanks;
    }

    /**
     * 获取敌方血量最低的坦克
     */
    public static Tank getMinShenMingEnemyTank(Tank myTank) {
        Tank tank = null;
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            Condition.gameMap.start = new Node(myTank.coord);
            Condition.gameMap.end = new Node(Condition.enemyArmata1.coord);
            if (AStar.canArrive(Condition.gameMap)) {
                tank = Condition.enemyArmata1;
            }
        }
        if ((tank == null && Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) ||
                (tank != null && Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > tank.shengyushengming)) {
            Condition.gameMap.start = new Node(myTank.coord);
            Condition.gameMap.end = new Node(Condition.enemyArmata2.coord);
            if (AStar.canArrive(Condition.gameMap)) {
                tank = Condition.enemyArmata2;
            }
        }

        if ((tank == null && Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) ||
                (tank != null && Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > tank.shengyushengming)) {
            Condition.gameMap.start = new Node(myTank.coord);
            Condition.gameMap.end = new Node(Condition.enemyT90.coord);
            if (AStar.canArrive(Condition.gameMap)) {
                tank = Condition.enemyT90;
            }
        }
        if ((tank == null && Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) ||
                (tank != null && Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > tank.shengyushengming)) {
            Condition.gameMap.start = new Node(myTank.coord);
            Condition.gameMap.end = new Node(Condition.enemyK2Panther.coord);
            if (AStar.canArrive(Condition.gameMap)) {
                tank = Condition.enemyK2Panther;
            }
        }
        if ((tank == null && Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) ||
                (tank != null && Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > tank.shengyushengming)) {
            Condition.gameMap.start = new Node(myTank.coord);
            Condition.gameMap.end = new Node(Condition.enemyWZ123.coord);
            if (AStar.canArrive(Condition.gameMap)) {
                tank = Condition.enemyWZ123;
            }
        }
        return tank;
    }

    /**
     * 从目前已发现的敌方坦克中寻找最优目标
     */
    public static Tank getBestEnemyTarget() {
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            return Condition.enemyWZ123;
        } else if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) {
            return Condition.enemyK2Panther;
        } else if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            return Condition.enemyArmata1;
        } else if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            return Condition.enemyArmata2;
        } else if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            return Condition.enemyT90;
        } else {
            return null;
        }
    }

    /**
     * 找出距离本方整体最近的敌方坦克
     */
    public static Tank getNearEnemyTarget() {
        Tank target = null;
        Integer distance = Integer.MAX_VALUE;

        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) {
            Integer toTankDistance = getAllMyToEnemyTankDistance(Condition.enemyK2Panther);
            if (toTankDistance < distance) {
                distance = toTankDistance;
                target = Condition.enemyK2Panther;
            }
        }
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            Integer toTankDistance = getAllMyToEnemyTankDistance(Condition.enemyWZ123);
            if (toTankDistance < distance) {
                distance = toTankDistance;
                target = Condition.enemyWZ123;
            }
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            Integer toTankDistance = getAllMyToEnemyTankDistance(Condition.enemyT90);
            if (toTankDistance < distance) {
                distance = toTankDistance;
                target = Condition.enemyT90;
            }
        }
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            Integer toTankDistance = getAllMyToEnemyTankDistance(Condition.enemyArmata1);
            if (toTankDistance < distance) {
                distance = toTankDistance;
                target = Condition.enemyArmata1;
            }
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            Integer toTankDistance = getAllMyToEnemyTankDistance(Condition.enemyArmata2);
            if (toTankDistance < distance) {
                distance = toTankDistance;
                target = Condition.enemyArmata2;
            }
        }
        if (Condition.npc.coord != null && Condition.npc.shengyushengming > 0) {
            //对NPC的距离优先级调低
            Integer toTankDistance = getAllMyToEnemyTankDistance(Condition.npc) + 4;
            if (toTankDistance < distance) {
                distance = toTankDistance;
                target = Condition.npc;
            }
        }
        return target;
    }

    /**
     * 找出我方坦克整体距离敌方坦克的距离
     */
    public static Integer getAllMyToEnemyTankDistance(Tank tank) {
        Integer distance = 0;
        if (Condition.myT90.shengyushengming > 0) {
            distance = distance + AStar.calcH(Condition.myT90.coord, tank.coord);
        }
        if (Condition.myK2Panther.shengyushengming > 0) {
            distance = distance + AStar.calcH(Condition.myK2Panther.coord, tank.coord);
        }
        if (Condition.myWZ123.shengyushengming > 0) {
            distance = distance + AStar.calcH(Condition.myWZ123.coord, tank.coord);
        }
        if (Condition.myArmata1.shengyushengming > 0) {
            distance = distance + (AStar.calcH(Condition.myArmata1.coord, tank.coord) / 2);
        }
        if (Condition.myArmata2.shengyushengming > 0) {
            distance = distance + (AStar.calcH(Condition.myArmata2.coord, tank.coord) / 2);
        }
        return distance;
    }

    /**
     * 是否已经发现了没死的敌人
     */
    public static boolean discoveryEnemy() {
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            return true;
        } else if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            return true;
        } else if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            return true;
        } else if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            return true;
        } else return Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0;

    }


    /**
     * 找出我方坦克能被敌方坦克攻击的数量
     */
    public static Integer getCanBeAttackNum(Coord myTankCoord) {
        return enemyCanAttackSum(myTankCoord).size();
    }

    /**
     * 包围攻击NPC
     */
    public static void surroundNPC(Tank myTank) {
        if (Condition.npc.coord != null && Condition.npc.shengyushengming > 0) {
            boolean canAttackNPC = myCanAttack(myTank, Condition.npc.coord);
            if (canAttackNPC) {
                myTank.tankStatus = TankStatus.ATTACK;
                myTank.targetDirection = getDirection(myTank.coord, Condition.npc.coord);
            } else {
                myTank.tankStatus = TankStatus.TARGET;
                myTank.targetCoord = Condition.npc.coord;
            }
        }
    }


    public static boolean discoveryNPC() {
        return (Condition.npc.coord != null && Condition.npc.shengyushengming > 0);
    }


    public static Coord findNextCoord(Tank myTank) {
        if (myTank.coord.equals(myTank.targetCoord)) {
            return null;
        }
        Condition.gameMap.start = new Node(myTank.coord);
        Condition.gameMap.end = new Node(myTank.targetCoord);
        Coord nextCoord = AStar.findNextCoord(Condition.gameMap);
        return nextCoord;
    }

    /**
     * 找出避开敌方火力的路线的下一步移动的坐标
     */
    public static Coord findNextCoordVoidEnemyFire(Tank myTank) {
        if (myTank.coord.equals(myTank.targetCoord)) {
            return null;
        }
        Condition.gameMap.start = new Node(myTank.coord);
        Condition.gameMap.end = new Node(myTank.targetCoord);
        Coord nextCoord = AStar.findNextCoordVoidEnemyFire(Condition.gameMap);
        return nextCoord;
    }

    public static DirectionAndDistance armataFindNextCoord(Tank myTank) {
        if (myTank.coord.equals(myTank.targetCoord)) {
            return null;
        }
        Condition.gameMap.start = new Node(myTank.coord);
        Condition.gameMap.end = new Node(myTank.targetCoord);
        DirectionAndDistance directionAndDistance = AStar.ArmataFindNextStep(Condition.gameMap);
        return directionAndDistance;
    }

    /**
     * 判断能攻击到指定坐标的敌方坦克的数量
     */
    public static List<Tank> enemyCanAttackSum(Coord coord) {
        List<Tank> enemyTanks = new ArrayList<>();
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0 && enemyCanAttack(Condition.enemyArmata1, coord)) {
            enemyTanks.add(Condition.enemyArmata1);
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0 && enemyCanAttack(Condition.enemyArmata2, coord)) {
            enemyTanks.add(Condition.enemyArmata2);
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0 && enemyCanAttack(Condition.enemyT90, coord)) {
            enemyTanks.add(Condition.enemyT90);
        }
        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0 && enemyCanAttack(Condition.enemyK2Panther, coord)) {
            enemyTanks.add(Condition.enemyK2Panther);
        }
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0 && enemyCanAttack(Condition.enemyWZ123, coord)) {
            enemyTanks.add(Condition.enemyWZ123);
        }
        //判断该坐标与敌方99式之间的关系
        coordAndEnemyWZ123(coord, enemyTanks);

        return enemyTanks;
    }

    /**
     * 判断坐标与敌方99式间的关系
     */
    public static void coordAndEnemyWZ123(Coord coord, List<Tank> enemyTanks) {

        if (enemyTanks.contains(Condition.enemyWZ123) || Condition.enemyWZ123.coord == null || Condition.enemyWZ123.shengyushengming <= 0) {
            return;
        }
        //    判断该坐标是否在敌方99式的攻击范围内
        if (coord.x == Condition.enemyWZ123.coord.x && Math.abs(coord.y - Condition.enemyWZ123.coord.y) <= Condition.enemyWZ123.shecheng) {
            //   横坐标相同
            if (coord.y > Condition.enemyWZ123.coord.y) {
                //我方在敌方99式下方
                Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y + 1);
                if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                    return;
                } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                    //判断敌方99式身边是否有己方坦克且不会跑路
                    return;
                }
                for (int i = 2; i <= Math.abs(coord.y - Condition.enemyWZ123.coord.y); i++) {
                    //如果之间有障碍物
                    if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y + i)) == BAR) {
                        return;
                    }
                }
                enemyTanks.add(Condition.enemyWZ123);
            } else {
                //我方在敌方99式上方
                Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y - 1);
                if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                    return;
                } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                    //判断敌方99式身边是否有己方坦克且不会跑路
                    return;
                }
                for (int i = 2; i <= Math.abs(coord.y - Condition.enemyWZ123.coord.y); i++) {
                    //如果之间有障碍物
                    if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y - i)) == BAR) {
                        return;
                    }
                }
                enemyTanks.add(Condition.enemyWZ123);
            }
        } else if (coord.y == Condition.enemyWZ123.coord.y && Math.abs(coord.x - Condition.enemyWZ123.coord.x) <= Condition.enemyWZ123.shecheng) {
            //  纵坐标相同
            if (coord.x > Condition.enemyWZ123.coord.x) {
                //我方在敌方99式右方
                Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x + 1, Condition.enemyWZ123.coord.y);
                if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                    return;
                } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                    //判断敌方99式身边是否有己方坦克且不会跑路
                    return;
                }
                for (int i = 2; i <= Math.abs(coord.x - Condition.enemyWZ123.coord.x); i++) {
                    //如果之间有障碍物
                    if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x + i, Condition.enemyWZ123.coord.y)) == BAR) {
                        return;
                    }
                }
                enemyTanks.add(Condition.enemyWZ123);
            } else {
                //我方在敌方99式左方
                Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x - 1, Condition.enemyWZ123.coord.y);
                if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                    return;
                } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                    //判断敌方99式身边是否有己方坦克且不会跑路
                    return;
                }
                for (int i = 2; i <= Math.abs(coord.x - Condition.enemyWZ123.coord.x); i++) {
                    //如果之间有障碍物
                    if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x - i, Condition.enemyWZ123.coord.y)) == BAR) {
                        return;
                    }
                }
                enemyTanks.add(Condition.enemyWZ123);
            }
        }
    }

    /**
     * 判断坐标与敌方99式间的关系
     */
    public static boolean coordAndEnemyWZ123(Coord coord) {
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            //    判断该坐标是否在敌方99式的攻击范围内
            if (coord.x == Condition.enemyWZ123.coord.x && Math.abs(coord.y - Condition.enemyWZ123.coord.y) <= Condition.enemyWZ123.shecheng) {
                //   横坐标相同
                if (coord.y > Condition.enemyWZ123.coord.y) {
                    //我方在敌方99式下方
                    Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y + 1);
                    if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                        return false;
                    } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                        //判断敌方99式身边是否有己方坦克且不会跑路
                        return false;
                    }
                    for (int i = 2; i <= Math.abs(coord.y - Condition.enemyWZ123.coord.y); i++) {
                        //如果之间有障碍物
                        if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y + i)) == BAR) {
                            return false;
                        }
                    }
                    return true;
                } else {
                    //我方在敌方99式上方
                    Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y - 1);
                    if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                        return false;
                    } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                        //判断敌方99式身边是否有己方坦克且不会跑路
                        return false;
                    }
                    for (int i = 2; i <= Math.abs(coord.y - Condition.enemyWZ123.coord.y); i++) {
                        //如果之间有障碍物
                        if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x, Condition.enemyWZ123.coord.y - i)) == BAR) {
                            return false;
                        }
                    }
                    return true;
                }
            } else if (coord.y == Condition.enemyWZ123.coord.y && Math.abs(coord.x - Condition.enemyWZ123.coord.x) <= Condition.enemyWZ123.shecheng) {
                //  纵坐标相同
                if (coord.x > Condition.enemyWZ123.coord.x) {
                    //我方在敌方99式右方
                    Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x + 1, Condition.enemyWZ123.coord.y);
                    if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                        return false;
                    } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                        //判断敌方99式身边是否有己方坦克且不会跑路
                        return false;
                    }
                    for (int i = 2; i <= Math.abs(coord.x - Condition.enemyWZ123.coord.x); i++) {
                        //如果之间有障碍物
                        if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x + i, Condition.enemyWZ123.coord.y)) == BAR) {
                            return false;
                        }
                    }
                    return true;
                } else {
                    //我方在敌方99式左方
                    Coord nearCoord = new Coord(Condition.enemyWZ123.coord.x - 1, Condition.enemyWZ123.coord.y);
                    if (Condition.gameMap.maps.get(nearCoord) == BAR || Condition.gameMap.maps.get(nearCoord) == NPC) {
                        return false;
                    } else if (isMyTank(Condition.gameMap.maps.get(nearCoord)) && getCanBeAttackNum(nearCoord) <= 1) {
                        //判断敌方99式身边是否有己方坦克且不会跑路
                        return false;
                    }
                    for (int i = 2; i <= Math.abs(coord.x - Condition.enemyWZ123.coord.x); i++) {
                        //如果之间有障碍物
                        if (Condition.gameMap.maps.get(new Coord(Condition.enemyWZ123.coord.x - i, Condition.enemyWZ123.coord.y)) == BAR) {
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断敌方坦克能否攻击到指定位置
     */
    public static boolean enemyCanAttack(Tank tank, Coord coord) {
        if (tank.coord.x == coord.x) {
            if (tank.shecheng >= Math.abs(tank.coord.y - coord.y)) {
                //坦克的坐标与目标点的横坐标距离相差比坦克的射程小
                if (tank.coord.y > coord.y) {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.y == tank.coord.y - i) {
                            return true;
                        }
                        int integer = enemyCanHaveValue(tank.coord.x, tank.coord.y - i);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                } else {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.y == tank.coord.y + i) {
                            return true;
                        }
                        int integer = enemyCanHaveValue(tank.coord.x, tank.coord.y + i);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                }
            }
        } else if (tank.coord.y == coord.y) {
            if (tank.shecheng >= Math.abs(tank.coord.x - coord.x)) {
                //坦克的坐标与目标点的横坐标距离相差比坦克的射程小
                if (tank.coord.x > coord.x) {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.x == tank.coord.x - i) {
                            return true;
                        }
                        int integer = enemyCanHaveValue(tank.coord.x - i, tank.coord.y);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                } else {
                    for (int i = 1; i < tank.shecheng + 1; i++) {
                        if (coord.x == tank.coord.x + i) {
                            return true;
                        }
                        int integer = enemyCanHaveValue(tank.coord.x + i, tank.coord.y);
                        if (integer == 0 || integer == 1) {
                            return false;
                        }
                    }
                }
            }
        } else {
            //横坐标、纵坐标都不相等，无法攻击到
            return false;
        }
        return false;
    }


    private static Integer enemyCanHaveValue(Integer x, Integer y) {
        // 是否在地图中
        if (x < 0 || x >= Condition.gameMap.width || y < 0 || y >= Condition.gameMap.hight) {
            return 0;
        }
        MapElement mapElement = Condition.gameMap.maps.get(new Coord(x, y));
        if (mapElement == MapElement.BAR) {
            return 0;
        }
        if (mapElement == MY_ARMATA1
                || mapElement == MapElement.MY_ARMATA2
                || mapElement == MapElement.MY_K2PANTHER
                || mapElement == MapElement.MY_T90
                || mapElement == MapElement.NPC
                || mapElement == MapElement.MY_WZ123) {
            return 1;
        }
        return 2;
    }

    public static Coord getNextCoord(DirectionAndDistance directionAndDistance, Coord coord) {
        Direction direction = directionAndDistance.getDirection();
        Integer distance = directionAndDistance.getDistance();
        if (direction == Direction.LEFT) {
            return new Coord(coord.x - distance, coord.y);
        } else if (direction == Direction.RIGHT) {
            return new Coord(coord.x + distance, coord.y);
        } else if (direction == Direction.UP) {
            return new Coord(coord.x, coord.y - distance);
        } else if (direction == Direction.DOWN) {
            return new Coord(coord.x, coord.y + distance);
        } else {
            return coord;
        }
    }

    /**
     * 找出正在和我方交战的敌方坦克
     */
    public static List<Tank> listFightingEnemyTanks() {
        List<Tank> tanks = new ArrayList<>();
        if (Condition.myArmata1.shengyushengming > 0) {
            tanks.addAll(MapUtil.canAttackEnemyList(Condition.myArmata1));
        }
        if (Condition.myArmata2.shengyushengming > 0) {
            tanks.addAll(MapUtil.canAttackEnemyList(Condition.myArmata2));
        }
        if (Condition.myT90.shengyushengming > 0) {
            tanks.addAll(MapUtil.canAttackEnemyList(Condition.myT90));
        }
        if (Condition.myK2Panther.shengyushengming > 0) {
            tanks.addAll(MapUtil.canAttackEnemyList(Condition.myK2Panther));
        }
        if (Condition.myWZ123.shengyushengming > 0) {
            tanks.addAll(MapUtil.canAttackEnemyList(Condition.myWZ123));
        }
        return tanks.stream().distinct().collect(Collectors.toList());
    }


    /**
     * 找出正在和我方坦克交战且距离最近的敌方坦克
     */
    public static Tank findMinDistanceAndInFireEnemy(Tank myTank) {
        List<Tank> tanks = listFightingEnemyTanks();
        if (tanks == null || tanks.isEmpty()) {
            return null;
        }
        //判断是否是敌方黑豹，如果是，判断是否正在被集火
        Optional<Tank> enemyK2PantherOptional = tanks.stream().filter(tank -> tank.tId.equals(Condition.enemyK2Panther.tId))
                .findAny();
        if (enemyK2PantherOptional.isPresent()) {
            Tank enemyK2Panther = enemyK2PantherOptional.get();
            Integer enemyK2PantherMyValue = Condition.gameMap.getMyValue(enemyK2Panther.coord);
            if (enemyK2PantherMyValue < 2) {
                tanks.remove(enemyK2Panther);
            }
            Integer enemyValueSum = MapUtil.getEnemyValueSum();
            Integer myValueSum = MapUtil.getMyValueSum();
            if (myValueSum < enemyValueSum) {
                //  判断己方集火坦克会不会跑路
                List<Tank> myCanAttackTanks = getAllMyCanAttack(enemyK2Panther.coord);
                myCanAttackTanks = myCanAttackTanks.stream().filter(tank -> {
                    Coord bestCoord = Condition.gameMap.myBestCoord(tank);
                    return getCanBeAttackNum(tank.coord) <= 1 || tank.coord.equals(bestCoord);
                }).collect(Collectors.toList());
                if (myCanAttackTanks.size() < 1) {
                    tanks.remove(enemyK2Panther);
                } else if (myCanAttackTanks.size() == 1 && !myCanAttackTanks.get(0).tId.equals(Condition.myK2Panther.tId)) {
                    tanks.remove(enemyK2Panther);
                }
            }

        }
        if (tanks.isEmpty()) {
            return null;
        }
        return tanks.stream().min(Comparator.comparingInt(value -> AStar.calcH(myTank.coord, value.coord))).get();
    }

    /**
     * 找到最佳集合坐标
     */
    public static Coord findBestFormationCoord() {
        Coord coord = Condition.gameMap.maps.keySet().stream().min(Comparator.comparingInt(formationCoord -> {
            Integer distance = 0;
            if (Condition.myArmata1.shengyushengming > 0) {
                distance = distance + AStar.calcH(formationCoord, Condition.myArmata1.coord) / 2;
            }
            if (Condition.myArmata2.shengyushengming > 0) {
                distance = distance + AStar.calcH(formationCoord, Condition.myArmata2.coord) / 2;
            }
            if (Condition.myT90.shengyushengming > 0) {
                distance = distance + AStar.calcH(formationCoord, Condition.myT90.coord);
            }
            if (Condition.myK2Panther.shengyushengming > 0) {
                distance = distance + AStar.calcH(formationCoord, Condition.myK2Panther.coord);
            }
            if (Condition.myWZ123.shengyushengming > 0) {
                distance = distance + AStar.calcH(formationCoord, Condition.myWZ123.coord);
            }
            return distance;
        })).get();
        if (coordIsBeLight(coord)) {
            return coord;
        } else {
            //    回出生点集合
            return Condition.formationCoord;
        }
    }

    /**
     * 判断坐标点周边范围是否已被点亮
     */
    public static boolean coordIsBeLight(Coord coord) {
        for (int x = coord.x - 3; x <= coord.x + 3; x++) {
            for (int y = coord.y - 3; y <= coord.y + 3; y++) {
                if (Condition.mistyCoords.contains(new Coord(x, y))) {
                    return false;
                }
            }
        }
        return true;
    }


    /**
     * 找出距离最近的敌方坦克且敌方坦克攻击距离为1
     */
    public static Tank getMinDistance1SheChengEnemyTank(Tank myTank) {
        Tank enemyTank = null;
        Integer distance = Integer.MAX_VALUE;
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            int enemyArmata1Distance = AStar.calcH(Condition.enemyArmata1.coord, myTank.coord);
            if (enemyArmata1Distance < distance) {
                enemyTank = Condition.enemyArmata1;
                distance = enemyArmata1Distance;
            }
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            int enemyArmata2Distance = AStar.calcH(Condition.enemyArmata2.coord, myTank.coord);
            if (enemyArmata2Distance < distance) {
                enemyTank = Condition.enemyArmata2;
                distance = enemyArmata2Distance;
            }
        }
        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) {
            int enemyK2PantherDistance = AStar.calcH(Condition.enemyK2Panther.coord, myTank.coord);
            if (enemyK2PantherDistance < distance) {
                enemyTank = Condition.enemyK2Panther;
                distance = enemyK2PantherDistance;
            }
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            int enemyT90Distance = AStar.calcH(Condition.enemyT90.coord, myTank.coord);
            if (enemyT90Distance < distance) {
                enemyTank = Condition.enemyT90;
                distance = enemyT90Distance;
            }
        }
        return enemyTank;
    }

    /**
     * 找出距离最近的敌方坦克
     */
    public static Tank getMinDistanceEnemyTank(Tank myTank) {
        Tank enemyTank = null;
        Integer distance = Integer.MAX_VALUE;
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            int enemyArmata1Distance = AStar.calcH(Condition.enemyArmata1.coord, myTank.coord);
            if (enemyArmata1Distance < distance) {
                enemyTank = Condition.enemyArmata1;
                distance = enemyArmata1Distance;
            }
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            int enemyArmata2Distance = AStar.calcH(Condition.enemyArmata2.coord, myTank.coord);
            if (enemyArmata2Distance < distance) {
                enemyTank = Condition.enemyArmata2;
                distance = enemyArmata2Distance;
            }
        }
        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) {
            int enemyK2PantherDistance = AStar.calcH(Condition.enemyK2Panther.coord, myTank.coord);
            if (enemyK2PantherDistance < distance) {
                enemyTank = Condition.enemyK2Panther;
                distance = enemyK2PantherDistance;
            }
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            int enemyT90Distance = AStar.calcH(Condition.enemyT90.coord, myTank.coord);
            if (enemyT90Distance < distance) {
                enemyTank = Condition.enemyT90;
                distance = enemyT90Distance;
            }
        }
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            int enemyWZ123Distance = AStar.calcH(Condition.enemyWZ123.coord, myTank.coord);
            if (enemyWZ123Distance < distance) {
                enemyTank = Condition.enemyWZ123;
                distance = enemyWZ123Distance;
            }
        }
        return enemyTank;
    }


    public static boolean isMyTank(MapElement mapElement) {
        return mapElement == MY_ARMATA1
                || mapElement == MY_ARMATA2
                || mapElement == MY_T90
                || mapElement == MY_K2PANTHER
                || mapElement == MY_WZ123;
    }

    public static boolean isEnemyTank(MapElement mapElement) {
        return mapElement == ENEMY_ARMATA1
                || mapElement == ENEMY_ARMATA2
                || mapElement == ENEMY_T90
                || mapElement == ENEMY_K2PANTHER
                || mapElement == ENEMY_WZ123;
    }

    /**
     * 该坐标友方是否可达
     */
    public static boolean coordCanArrive(Coord nextCoord) {
        MapElement mapElement = Condition.gameMap.maps.get(nextCoord);
        return mapElement == WAY
                || mapElement == MISTY
                || mapElement == RESURGENCE;
    }

    /**
     * 判断赢了还是输了
     */
    public static boolean isWin() {
        Integer myShengyushengming = Condition.myArmata1.shengyushengming + Condition.myArmata2.shengyushengming + Condition.myT90.shengyushengming + Condition.myK2Panther.shengyushengming + Condition.myWZ123.shengyushengming;
        Integer enemyShengyushengming = Condition.enemyArmata1.shengyushengming + Condition.enemyArmata2.shengyushengming + Condition.enemyT90.shengyushengming + Condition.enemyK2Panther.shengyushengming + Condition.enemyWZ123.shengyushengming;
        //    判断用了复活币之后的生命
        if (Condition.myGoldNum > 0) {
            Tank tank = findBestResurgenceTank();
            if (tank != null) {
                myShengyushengming = myShengyushengming + (tank.shengming - tank.shengyushengming);
            }
        }

        if (!myShengyushengming.equals(enemyShengyushengming)) {
            return myShengyushengming > enemyShengyushengming;
        }
        //    双方生命值相等
        return Condition.myExtend >= Condition.enemyExtend;
    }

    /**
     * 找出最适合复活的坦克
     */
    public static Tank findBestResurgenceTank() {
        Tank bestTank = null;
        Integer recoverBlood = 0;
        int myArmata1RecoverBlood = Condition.myArmata1.shengming - Condition.myArmata1.shengyushengming;
        if (myArmata1RecoverBlood > recoverBlood) {
            bestTank = Condition.myArmata1;
            recoverBlood = myArmata1RecoverBlood;
        }
        int myArmata2RecoverBlood = Condition.myArmata2.shengming - Condition.myArmata2.shengyushengming;
        if (myArmata2RecoverBlood > recoverBlood) {
            bestTank = Condition.myArmata2;
            recoverBlood = myArmata2RecoverBlood;
        }
        int myT90RecoverBlood = Condition.myT90.shengming - Condition.myT90.shengyushengming;
        if (myT90RecoverBlood > recoverBlood) {
            bestTank = Condition.myT90;
            recoverBlood = myT90RecoverBlood;
        }
        int myK2PantherRecoverBlood = Condition.myK2Panther.shengming - Condition.myK2Panther.shengyushengming;
        if (myK2PantherRecoverBlood > recoverBlood) {
            bestTank = Condition.myK2Panther;
            recoverBlood = myK2PantherRecoverBlood;
        }
        int myWZ123RecoverBlood = Condition.myWZ123.shengming - Condition.myWZ123.shengyushengming;
        if (myWZ123RecoverBlood > recoverBlood) {
            bestTank = Condition.myWZ123;
            recoverBlood = myWZ123RecoverBlood;
        }
        return bestTank;
    }

    /**
     * 获取所有敌方坦克身边距离为1的坐标
     */
    public static List<Coord> listNearEnemyTankCoords() {
        List<Coord> coords = new ArrayList<>();
        if (Condition.enemyArmata1.coord != null && Condition.enemyArmata1.shengyushengming > 0) {
            coords.addAll(listNearCanArriveCoords(Condition.enemyArmata1.coord));
        }
        if (Condition.enemyArmata2.coord != null && Condition.enemyArmata2.shengyushengming > 0) {
            coords.addAll(listNearCanArriveCoords(Condition.enemyArmata2.coord));
        }
        if (Condition.enemyWZ123.coord != null && Condition.enemyWZ123.shengyushengming > 0) {
            coords.addAll(listNearCanArriveCoords(Condition.enemyWZ123.coord));
        }
        if (Condition.enemyT90.coord != null && Condition.enemyT90.shengyushengming > 0) {
            coords.addAll(listNearCanArriveCoords(Condition.enemyT90.coord));
        }
        if (Condition.enemyK2Panther.coord != null && Condition.enemyK2Panther.shengyushengming > 0) {
            coords.addAll(listNearCanArriveCoords(Condition.enemyK2Panther.coord));
        }
        return coords;
    }

    public static List<Coord> listNearCanArriveCoords(Coord coord) {
        List<Coord> coords = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            //威胁有4个方向
            if (i == 0) {
                //左
                Coord myTankCoord = new Coord(coord.x - 1, coord.y);
                if (coordCanArrive(myTankCoord)) {
                    coords.add(myTankCoord);
                }
            } else if (i == 1) {
                //    右
                Coord myTankCoord = new Coord(coord.x + 1, coord.y);
                if (coordCanArrive(myTankCoord)) {
                    coords.add(myTankCoord);
                }
            } else if (i == 2) {
                //    下
                Coord myTankCoord = new Coord(coord.x, coord.y - 1);
                if (coordCanArrive(myTankCoord)) {
                    coords.add(myTankCoord);
                }
            } else {
                //    上
                Coord myTankCoord = new Coord(coord.x, coord.y + 1);
                if (coordCanArrive(myTankCoord)) {
                    coords.add(myTankCoord);
                }
            }
        }
        return coords;
    }

    public static List<Coord> listNearCanArriveCoords(Tank tank) {
        Coord coord = tank.oldCoord;
        List<Coord> coords = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= tank.yidong; j++) {
                //威胁有4个方向
                if (i == 0) {
                    //左
                    Coord myTankCoord = new Coord(coord.x - j, coord.y);
                    if (coordCanArrive(myTankCoord)) {
                        coords.add(myTankCoord);
                    } else {
                        break;
                    }
                } else if (i == 1) {
                    //    右
                    Coord myTankCoord = new Coord(coord.x + j, coord.y);
                    if (coordCanArrive(myTankCoord)) {
                        coords.add(myTankCoord);
                    } else {
                        break;
                    }
                } else if (i == 2) {
                    //    上
                    Coord myTankCoord = new Coord(coord.x, coord.y - j);
                    if (coordCanArrive(myTankCoord)) {
                        coords.add(myTankCoord);
                    } else {
                        break;
                    }
                } else {
                    //    下
                    Coord myTankCoord = new Coord(coord.x, coord.y + j);
                    if (coordCanArrive(myTankCoord)) {
                        coords.add(myTankCoord);
                    } else {
                        break;
                    }
                }
            }
        }
        return coords;
    }

    public static List<Coord> listNearCanBeWZ123AttackCoords(Coord coord) {
        List<Coord> coords = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 2; j <= 3; j++) {
                //威胁有4个方向
                if (i == 0) {
                    //左
                    Integer value = myCanHaveValue(coord.x - j, coord.y);
                    if (value == 0) {
                        break;
                    } else {
                        coords.add(new Coord(coord.x - j, coord.y));
                    }
                } else if (i == 1) {
                    //    右
                    Integer value = myCanHaveValue(coord.x + j, coord.y);
                    if (value == 0) {
                        break;
                    } else {
                        coords.add(new Coord(coord.x + j, coord.y));
                    }
                } else if (i == 2) {
                    //    上
                    Integer value = myCanHaveValue(coord.x, coord.y - j);
                    if (value == 0) {
                        break;
                    } else {
                        coords.add(new Coord(coord.x, coord.y - j));
                    }
                } else {
                    //    下
                    Integer value = myCanHaveValue(coord.x, coord.y + j);
                    if (value == 0) {
                        break;
                    } else {
                        coords.add(new Coord(coord.x, coord.y + j));
                    }
                }
            }
        }
        return coords;
    }

    public static void refreshAssistedNPC() {
        if (Condition.npc.coord != null && Condition.npc.shengyushengming >= 0) {
            Integer npcMyValue = Condition.gameMap.getMyValue(Condition.npc.coord);
            //    可以攻击到NPC，判断npc受到的己方威胁值能不能一炮解决
            if (npcMyValue >= Condition.npc.shengyushengming) {
                Condition.assistedNPC = true;
            }
        }
    }

    /**
     * 获取可以围杀的敌方坦克
     */
    public static List<TankAndCoord> canWeiShaAloneEnemyTank(Tank enemyTank) {
        List<TankAndCoord> tankAndCoords = new ArrayList<>();
        if (enemyTank != null) {
            List<Coord> enemyCanBeAttackCoords = listNearCanArriveCoords(enemyTank.coord);
            List<Coord> enemyCanBeAttackWZ123Coords = listNearCanBeWZ123AttackCoords(enemyTank.coord);

            List<Coord> myWZ123CanArriveCoords = listNearCanArriveCoords(Condition.myWZ123);
            myWZ123CanArriveCoords.retainAll(enemyCanBeAttackWZ123Coords);
            if (!myWZ123CanArriveCoords.isEmpty() && (enemyTank.shecheng <= 1 || Condition.myWZ123.shengyushengming > enemyTank.gongji)) {
                Coord coord = myWZ123CanArriveCoords.stream().max(Comparator.comparingInt(value -> {
                    Integer distance = 0;
                    if (Condition.myArmata2.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata2.coord, enemyTank.coord);
                    }
                    if (Condition.myT90.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myT90.coord, enemyTank.coord);
                    }
                    if (Condition.myK2Panther.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myK2Panther.coord, enemyTank.coord);
                    }
                    if (Condition.myArmata1.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata1.coord, enemyTank.coord);
                    }
                    return distance;
                })).get();
                if (Condition.gameMap.getEnemyValue(coord) <= 0) {
                    tankAndCoords.add(TankAndCoord.builder().tank(Condition.myWZ123).coord(coord).build());
                    enemyCanBeAttackCoords.remove(coord);
                    enemyCanBeAttackWZ123Coords.remove(coord);
                }
            }

            List<Coord> myK2PantherCanArriveCoords = listNearCanArriveCoords(Condition.myK2Panther);
            myK2PantherCanArriveCoords.retainAll(enemyCanBeAttackCoords);
            if (!myK2PantherCanArriveCoords.isEmpty() && Condition.myK2Panther.shengyushengming > enemyTank.gongji) {
                Coord coord = myK2PantherCanArriveCoords.stream().max(Comparator.comparingInt(value -> {
                    Integer distance = 0;
                    if (Condition.myArmata2.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata2.coord, enemyTank.coord);
                    }
                    if (Condition.myT90.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myT90.coord, enemyTank.coord);
                    }
                    if (Condition.myArmata1.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata1.coord, enemyTank.coord);
                    }
                    if (Condition.myWZ123.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myWZ123.coord, enemyTank.coord);
                    }
                    return distance;
                })).get();
                if (Condition.gameMap.getEnemyValue(coord) <= enemyTank.gongji) {
                    tankAndCoords.add(TankAndCoord.builder().tank(Condition.myK2Panther).coord(coord).build());
                    enemyCanBeAttackCoords.remove(coord);
                    enemyCanBeAttackWZ123Coords.remove(coord);
                }
            }
            List<Coord> myT90CanArriveCoords = listNearCanArriveCoords(Condition.myT90);
            myT90CanArriveCoords.retainAll(enemyCanBeAttackCoords);
            if (!myT90CanArriveCoords.isEmpty() && Condition.myT90.shengyushengming > enemyTank.gongji) {
                Coord coord = myT90CanArriveCoords.stream().max(Comparator.comparingInt(value -> {
                    Integer distance = 0;
                    if (Condition.myArmata2.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata2.coord, enemyTank.coord);
                    }
                    if (Condition.myArmata1.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata1.coord, enemyTank.coord);
                    }
                    if (Condition.myK2Panther.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myK2Panther.coord, enemyTank.coord);
                    }
                    if (Condition.myWZ123.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myWZ123.coord, enemyTank.coord);
                    }
                    return distance;
                })).get();
                if (Condition.gameMap.getEnemyValue(coord) <= enemyTank.gongji) {
                    tankAndCoords.add(TankAndCoord.builder().tank(Condition.myT90).coord(coord).build());
                    enemyCanBeAttackCoords.remove(coord);
                    enemyCanBeAttackWZ123Coords.remove(coord);
                }
            }
            List<Coord> myArmata1CanArriveCoords = listNearCanArriveCoords(Condition.myArmata1);
            myArmata1CanArriveCoords.retainAll(enemyCanBeAttackCoords);
            if (!myArmata1CanArriveCoords.isEmpty() && Condition.myArmata1.shengyushengming > enemyTank.gongji) {
                Coord coord = myArmata1CanArriveCoords.stream().max(Comparator.comparingInt(value -> {
                    Integer distance = 0;
                    if (Condition.myArmata2.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata2.coord, enemyTank.coord);
                    }
                    if (Condition.myT90.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myT90.coord, enemyTank.coord);
                    }
                    if (Condition.myK2Panther.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myK2Panther.coord, enemyTank.coord);
                    }
                    if (Condition.myWZ123.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myWZ123.coord, enemyTank.coord);
                    }
                    return distance;
                })).get();
                if (Condition.gameMap.getEnemyValue(coord) <= enemyTank.gongji) {
                    tankAndCoords.add(TankAndCoord.builder().tank(Condition.myArmata1).coord(coord).build());
                    enemyCanBeAttackCoords.remove(coord);
                    enemyCanBeAttackWZ123Coords.remove(coord);
                }
            }
            List<Coord> myArmata2CanArriveCoords = listNearCanArriveCoords(Condition.myArmata2);
            myArmata2CanArriveCoords.retainAll(enemyCanBeAttackCoords);
            if (!myArmata2CanArriveCoords.isEmpty() && Condition.myArmata2.shengyushengming > enemyTank.gongji) {
                Coord coord = myArmata2CanArriveCoords.stream().max(Comparator.comparingInt(value -> {
                    Integer distance = 0;
                    if (Condition.myArmata1.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myArmata1.coord, enemyTank.coord);
                    }
                    if (Condition.myT90.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myT90.coord, enemyTank.coord);
                    }
                    if (Condition.myK2Panther.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myK2Panther.coord, enemyTank.coord);
                    }
                    if (Condition.myWZ123.shengyushengming > 0) {
                        distance = distance + AStar.calcH(Condition.myWZ123.coord, enemyTank.coord);
                    }
                    return distance;
                })).get();
                if (Condition.gameMap.getEnemyValue(coord) <= enemyTank.gongji) {
                    tankAndCoords.add(TankAndCoord.builder().tank(Condition.myArmata2).coord(coord).build());
                    enemyCanBeAttackCoords.remove(coord);
                    enemyCanBeAttackWZ123Coords.remove(coord);
                }
            }

        }
        return tankAndCoords;
    }


    /**
     * 找出距离最近的敌方落单坦克
     */
    public static Tank findMinDistanceAloneEnemyTank() {
        List<Tank> enemyTanks = findAloneEnemyTank();
        if (enemyTanks != null && !enemyTanks.isEmpty()) {
            return enemyTanks.stream().min(Comparator.comparingInt(enemyTank -> {
                Integer distance = 0;
                if (Condition.myArmata1.shengyushengming > 0) {
                    distance = distance + AStar.calcH(Condition.myArmata1.coord, enemyTank.coord) / 2;
                }
                if (Condition.myArmata2.shengyushengming > 0) {
                    distance = distance + AStar.calcH(Condition.myArmata2.coord, enemyTank.coord) / 2;
                }
                if (Condition.myT90.shengyushengming > 0) {
                    distance = distance + AStar.calcH(Condition.myT90.coord, enemyTank.coord);
                }
                if (Condition.myK2Panther.shengyushengming > 0) {
                    distance = distance + AStar.calcH(Condition.myK2Panther.coord, enemyTank.coord);
                }
                if (Condition.myWZ123.shengyushengming > 0) {
                    distance = distance + AStar.calcH(Condition.myWZ123.coord, enemyTank.coord);
                }
                return distance;
            })).get();
        }
        return null;
    }

    /**
     * 找到落单的敌方坦克
     */
    public static List<Tank> findAloneEnemyTank() {
        List<Tank> enemyTanks = new ArrayList<>();
        //    判断敌方所有坦克都出现了
        if ((Condition.enemyArmata1.coord != null || Condition.enemyArmata1.shengyushengming <= 0)
                && (Condition.enemyArmata2.coord != null || Condition.enemyArmata2.shengyushengming <= 0)
                && (Condition.enemyT90.coord != null || Condition.enemyT90.shengyushengming <= 0)
                && (Condition.enemyK2Panther.coord != null || Condition.enemyK2Panther.shengyushengming <= 0)
                && (Condition.enemyWZ123.coord != null || Condition.enemyWZ123.shengyushengming <= 0)
        ) {
            if (Condition.enemyArmata1.shengyushengming > 0
                    && (Condition.enemyArmata2.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata1.coord, Condition.enemyArmata2.coord)))
                    && (Condition.enemyT90.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata1.coord, Condition.enemyT90.coord)))
                    && (Condition.enemyK2Panther.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata1.coord, Condition.enemyK2Panther.coord)))
                    && (Condition.enemyWZ123.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata1.coord, Condition.enemyWZ123.coord)))
            ) {
                enemyTanks.add(Condition.enemyArmata1);
            }
            if (Condition.enemyT90.shengyushengming > 0
                    && (Condition.enemyArmata2.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyT90.coord, Condition.enemyArmata2.coord)))
                    && (Condition.enemyArmata1.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyT90.coord, Condition.enemyArmata1.coord)))
                    && (Condition.enemyK2Panther.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyT90.coord, Condition.enemyK2Panther.coord)))
                    && (Condition.enemyWZ123.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyT90.coord, Condition.enemyWZ123.coord)))
            ) {
                enemyTanks.add(Condition.enemyT90);
            }
            if (Condition.enemyArmata2.shengyushengming > 0
                    && (Condition.enemyK2Panther.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata2.coord, Condition.enemyK2Panther.coord)))
                    && (Condition.enemyArmata1.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata2.coord, Condition.enemyArmata1.coord)))
                    && (Condition.enemyT90.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata2.coord, Condition.enemyT90.coord)))
                    && (Condition.enemyWZ123.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyArmata2.coord, Condition.enemyWZ123.coord)))
            ) {
                enemyTanks.add(Condition.enemyArmata2);
            }

            if (Condition.enemyWZ123.shengyushengming > 0
                    && (Condition.enemyArmata2.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyWZ123.coord, Condition.enemyArmata2.coord)))
                    && (Condition.enemyArmata1.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyWZ123.coord, Condition.enemyArmata1.coord)))
                    && (Condition.enemyT90.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyWZ123.coord, Condition.enemyT90.coord)))
                    && (Condition.enemyK2Panther.shengyushengming <= 0 || isFar(getSqrt(Condition.enemyWZ123.coord, Condition.enemyK2Panther.coord)))
            ) {
                enemyTanks.add(Condition.enemyWZ123);
            }


        }
        return enemyTanks;
    }

    public static boolean isFar(double distance) {
        return distance > 4;
    }

}
